package com.serialization;

import com.serialization.dto.User1;
import org.apache.poi.util.IOUtils;

import java.io.*;

/**
 * @author zhuangqi
 * @@date 2017/3/16.
 */
public class SerializableDemo1 {

    public static void main(String[] args) {

        User1 user = new User1();
        user.setName("zhuangqi");
        user.setAge(25);

        //写入文件
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("tempFile.txt"));
            oos.writeObject(user);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(oos);
        }

        //读取文件
        File file = new File("tempFile.txt");
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(file));
            User1 readuser = (User1) ois.readObject();
            System.out.println(readuser);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
